package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 全省通办清单
 */

public class QuanShengTongBanList extends Base {

    private final AllInfo allInfo = new AllInfo();
    private final StringBuilder region = new StringBuilder();
    private String listUrl;
    private String listStrResponse;



    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {

        //=====================================高频通办===========================================
        this.region.append("海南省-");
        String listUrl = "https://wssp.hainan.gov.cn/wssp3/zwgk/generalInfo";
        JSONObject listHeader = new JSONObject();
        listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/qstb");
        listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
        JSONObject listParmas = new JSONObject();
        listParmas.put("sxName", "");
        String listStrResponse = postFormReq(listUrl,listHeader,listParmas);
        JSONObject listResponse = JSONObject.parseObject(listStrResponse);
        this.listUrl = listUrl;
        this.listStrResponse = listStrResponse;
        for (Object o : listResponse.getJSONObject("data").getJSONArray("list")){
            JSONObject sxfl_mcList = (JSONObject) o;

            for (Object o1 : sxfl_mcList.getJSONArray("list")){
                JSONObject sx_mcList = (JSONObject) o1;
                String taskname = sx_mcList.getString("sx_mc");
                String topicid = sx_mcList.getString("sxfl_bm");

                //递归
                traversRegionAndGetInfo("HZ2881f4424539dd0142453c856b0025",topicid,taskname);
            }
        }


        //模块2
        //=====================================全流程网办===========================================
        //当前页
        int currentPage = 1;
        //总页数
        int pags = 0;
        while (currentPage != pags) {
            String listUrl2 = "https://wssp.hainan.gov.cn/wssp3/zwgk/qstbQlcwbSxQdList";
            JSONObject listHeader2 = new JSONObject();
            listHeader2.put("Referer", "https://wssp.hainan.gov.cn/hnwt/qstb");
            listHeader2.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            JSONObject listParmas2 = new JSONObject();
            listParmas2.put("dept", "");
            listParmas2.put("regionId", "HZ2881f4424539dd0142453c856b0025");
            listParmas2.put("taskName", "");
            listParmas2.put("pageNumber", currentPage);
            listParmas2.put("pageSize", "10");
            String listStrResponse2 = postFormReq(listUrl2, listHeader2, listParmas2);
            JSONObject listResponse2 = JSONObject.parseObject(listStrResponse2);
            for (Object o : listResponse2.getJSONObject("data").getJSONArray("list")){
                JSONObject reqData = (JSONObject) o;
                String id = reqData.getString("id");
                String url = "https://wssp.hainan.gov.cn/wssp3/bszn/exempt/allInfo";
                JSONObject header = new JSONObject();
                header.put("Referer", "https://wssp.hainan.gov.cn/hnwt/handlingGuideline?id=" + id);
                header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                JSONObject parmas = new JSONObject();
                parmas.put("id", id);
                allInfo.setAllInfoUrl(url);
                allInfo.setAllInfoHeader(header);
                allInfo.setAllInfoParmas(parmas);
                JSONObject result;
                try {
                    result = allInfo.getAllInfo();
                } catch (Exception e) {
                    break;
                }
                System.out.println(result);
                return processResult(
                        null,
                        null,
                        null,
                        allInfo.getResponce(),
                        result,
                        "海南省",
                        "政务公开-全省通办-全流程网办"
                );
            }
            //更新当前页和总页数
            currentPage++;
            pags = listResponse2.getJSONObject("data").getJSONObject("page").getInteger("pageCount") + 1;
        }
        return null;
    }
    //================递归遍历地区并且获取详情页数据=================
    public JSONObject traversRegionAndGetInfo(String xzqhid, String topicid, String taskname){
        String RegUrl = "https://wssp.hainan.gov.cn/wssp3/heatTheme/getHighlightAreaByTopicidAndXzqhid?";
        JSONObject RegHeader = new JSONObject();
        RegHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/qstb");
        RegHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
        JSONObject RegParmas = new JSONObject();
        RegParmas.put("xzqhid", xzqhid);
        RegParmas.put("topicid", topicid);
        RegParmas.put("taskname", taskname);
        String RegStrResponse = getReq(RegUrl, RegHeader, RegParmas);
        JSONObject RegResponse = JSONObject.parseObject(RegStrResponse);
        if (null != RegResponse.getJSONObject("data")){
            for (Object o : RegResponse.getJSONObject("data").getJSONArray("resultList")) {
                JSONObject result = (JSONObject) o;
                if (result.getString("ishighlight").equals("1")) {
                    //获取地区分级
                    this.region.append(result.getString("deptname")).append("-");

                    //递归遍历地区
                    traversRegionAndGetInfo(result.getString("deptid"), topicid, taskname);
                }
            }
        }else {
            //获取当前分级地区字符串长度
            int regionLength = this.region.length();
            //删掉最后一个“-”符号
            this.region.delete(regionLength-1,regionLength);
            //转String
            String reg = this.region.toString();
            //删除“海南省-”之后的字符串更新分级地区字符串
            this.region.delete(4,regionLength);
            //当前页
            int currentPage = 1;
            //总页数
            int pags = 0;
            while (currentPage != pags){
                String listPageUrl = "https://wssp.hainan.gov.cn/wssp3/heatTheme/getMatterByIdAndCode";
                JSONObject listPageHeader = new JSONObject();
                listPageHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/qstb");
                listPageHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                JSONObject listPageParmas = new JSONObject();
                listPageParmas.put("topicid", topicid);
                listPageParmas.put("xzqhid", xzqhid);
                listPageParmas.put("pageNum", currentPage);
                listPageParmas.put("pageSize", "1");
                listPageParmas.put("taskname", taskname);
                String listPageStrResponse = postFormReq(listPageUrl,listPageHeader, listPageParmas);
                JSONObject listPageResponse = JSONObject.parseObject(listPageStrResponse);

                for (Object  o : listPageResponse.getJSONObject("data").getJSONArray("resultList")){
                    JSONObject reqData = (JSONObject) o;
                    String rowguid = reqData.getString("rowguid");
                    String url = "https://wssp.hainan.gov.cn/wssp3/bszn/exempt/allInfo";
                    JSONObject header = new JSONObject();
                    header.put("Referer", "https://wssp.hainan.gov.cn/hnwt/handlingGuideline?rowguid=" + rowguid);
                    header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                    JSONObject parmas = new JSONObject();
                    parmas.put("rowguid", rowguid);
                    allInfo.setAllInfoUrl(url);
                    allInfo.setAllInfoHeader(header);
                    allInfo.setAllInfoParmas(parmas);
                    JSONObject result;
                    try {
                        result = allInfo.getAllInfo();
                    } catch (Exception e) {
                        break;
                    }
                    url = "https://wssp.hainan.gov.cn/hnwt/handlingGuideline?rowguid=" + rowguid;
                    result.put("地址", url);
                    System.out.println(result);//测试用代码
                    return processResult(
                            null,
                            null,
                            url,
                            allInfo.getResponce(),
                            result,
                            reg,
                            "全省通办-高频通办"
                    );
                }
                //更新当前页和总页数
                currentPage++;
                pags = listPageResponse.getJSONObject("data").getJSONObject("pageMap").getInteger("pages") + 1;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        QuanShengTongBanList quanShengTongBanList = new QuanShengTongBanList();
        quanShengTongBanList.getDataDemo();
    }
}
